package com.dk.aop;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoggerAop {

    private final Logger logger = LoggerFactory.getLogger(LoggerAop.class);

    @Pointcut("execution(public * com.dk.service..*.*(..)))")
    public void doPoint(){};

    // 前置通知
    @After("doPoint()")
    public void doAfter(JoinPoint joinPoint) {
        // 获取参数
        Object[] args = joinPoint.getArgs();
        // 获取方法
        Signature signature = joinPoint.getSignature();
        logger.info("执行的方法为：{}，参数为：{}",signature,args);
    }

    @AfterReturning(pointcut = "doPoint()",returning = "ret")
    public void doAfterReturning(Object ret) {
        String jsonString = JSONObject.toJSONString(ret);
        logger.info("方法执行成功返回的结果是:{}",jsonString);
    }
}
